package org.gnyxy.service.impl;

import org.gnyxy.constant.DefaultConstant;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.context.BaseContext;
import org.gnyxy.dto.AddressDTO;
import org.gnyxy.entity.Address;
import org.gnyxy.entity.AddressDic;
import org.gnyxy.exception.BaseException;
import org.gnyxy.mapper.AddressDicMapper;
import org.gnyxy.mapper.AddressMapper;
import org.gnyxy.service.AddressService;
import org.gnyxy.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 新增地址
     * @param addressDTO
     */
    @Override
    public void add(AddressDTO addressDTO) {
        // 校验
        if (addressDTO == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        // Bean拷贝
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        // 设置用户id 非默认地址 不删除
        address.setUserId(BaseContext.getCurrentId())
                .setIdDefault(DefaultConstant.IS_NOT_DEFAULT)
                .setDeleted(DeleteConstant.IS_NOT_DELETED);

        List<AddressVO> addressVOS = listByUserId(BaseContext.getCurrentId());
        if (addressVOS == null || addressVOS.size() == 0){
            address.setIdDefault(DefaultConstant.IS_DEFAULT);
        }
        // 插入
        addressMapper.insert(address);
    }

    /**
     * 根据UserId查询地址
     * @param currentId
     * @return
     */
    @Override
    public List<AddressVO> listByUserId(Long currentId) {
        // 校验
        if (currentId == null){
            throw new BaseException(MessageConstant.USER_NOT_LOGIN);
        }
        // 根据id查找
        return addressMapper.selectByUserId(currentId);
    }

    /**
     * 查询当前用户的默认
     * @param currentId
     * @return
     */
    @Override
    public AddressVO getDefault(Long currentId) {
        // 校验
        if (currentId == null){
            throw new BaseException(MessageConstant.USER_NOT_LOGIN);
        }
        // 根据id查找
        return addressMapper.selectDefaultByUserId(currentId);
    }

    /**
     * 根据id获取地址
     * @param id
     * @return
     */
    @Override
    public AddressVO getById(Long id) {
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        return addressMapper.selectById(id);
    }

    /**
     * 更新
     * @param address
     */
    @Override
    public void update(Address address) {
        if (address == null || address.getId() == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        addressMapper.update(address);
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 校验
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        Address address = new Address().setId(id)
                .setDeleted(DeleteConstant.IS_DELETED);
        addressMapper.update(address);
    }

    /**
     * 设置默认地址
     * @param id
     */
    @Override
    @Transactional
    public void setDefault(Long id) {
        // 校验
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        // 更新之前默认的为非默认
        AddressVO addressVO = addressMapper.selectDefaultByUserId(BaseContext.getCurrentId());
        if (addressVO != null){
            Address address = new Address();
            BeanUtils.copyProperties(addressVO,address);
            address.setIdDefault(DefaultConstant.IS_NOT_DEFAULT);
            addressMapper.update(address);
        }
        // 新设置非默认的为默认
        Address aDefault = new Address().setId(id)
                .setIdDefault(DefaultConstant.IS_DEFAULT);
        addressMapper.update(aDefault);
    }
}
